<template>
    <transition name="fade">
        <div class="login"></div>
    </transition>
</template>

<script>
import { Dialog, Toast } from "vant";
export default {
    name: "Login",
    data() {
        return {
            redirect: "/",
        };
    },
    mounted() {
        // this.getLogin("P6x6wvfKW0gw05SwKEhqxalD-Ea7rUq7BoPHwe-9f7M");
        // return
        if (this.getQueryVariable("code")) {
            Toast.loading({
                message: "加载中...",
                forbidClick: true,
                duration: 0,
            });
            // console.log(this.getQueryVariable("code"))
            this.getLogin(this.getQueryVariable("code"));
        } else {
            this.login();
        }
    },
    methods: {
        login() {
            let appid = "wwbe514664adcfafa1";
            let redirect_uri = process.env.VUE_APP_URL;
            let url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${encodeURI(appid)}&redirect_uri=${encodeURI(redirect_uri)}&response_type=code&scope=snsapi_base#wechat_redirect`;
            window.location.replace(url);
        },
        //获取地址栏参数
        getQueryVariable(variable) {
            var query = window.location.search.substring(1);
            var vars = query.split("&");
            for (var i = 0; i < vars.length; i++) {
                var pair = vars[i].split("=");
                if (pair[0] == variable) {
                    return pair[1];
                }
            }
            return false;
        },
        getLogin(code) {
            this.$api
                .login(code)
                .then((res) => {
                    console.log(res, "111");
                    Toast.clear();
                    if (res.code != 1) {
                        Dialog({ message: res.msg });
                        return;
                    }
                    localStorage.setItem("_token_", res.data);
                    this.$api.getList().then((res) => {
                        if (res.code == 1) {
                            localStorage.setItem("_type_", res.data.type);
                            // return;
                            // if (res.data.type == 3) {
                            //     window.location.replace(process.env.VUE_APP_HOME2 + `?info=${encodeURIComponent(JSON.stringify(res.data.lists))}`);
                            // } else {
                            //     window.location.replace(process.env.VUE_APP_HOME);
                            // }
                            window.location.replace(process.env.VUE_APP_HOME);
                        } else {
                            Toast(res.msg);
                        }
                    });
                })
                .catch((e) => {
                    console.log(e);
                    Toast("接口错误");
                });
        },
    },
};
</script>

<style lang="scss" scoped>
.login {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 2;
    background-color: white;
}

.fade-enter-active,
.fade-leave-active {
    transition: all 0.3s;
}

.fade-enter,
.fade-leave-to {
    transform: translateX(100%);
}
</style>
